/*-
 * RP_ACK.java     v2.0  12-Dec-2004
 *
 * $LastChangedBy: stein $
 * $LastChangedRevision: 96 $
 * $LastChangedDate: 2007-02-15 19:53:47 +0000 (Thu, 15 Feb 2007) $
 * $HeadURL: svn://localhost/SMS/src/com/googlecode/attention/smrl/RP_ACK.java $
 *
 * Copyright (c) 2007, Stein Gunnar Bakkeby
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Stein Gunnar Bakkeby nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.attention.smrl;

import com.googlecode.attention.sms.pdu.MTI;
import com.googlecode.attention.sms.pdu.SMSDeliverReport;
import com.googlecode.attention.sms.pdu.SMSSubmitReport;
import com.googlecode.attention.sms.pdu.TPDU;
import com.googlecode.attention.util.Convert;


/** * RP-ACK. * <BR> * <BR>The RP-ACK contains the RP-User-Data which is a parameter * containing the TPDU. *  * @author Stein Gunnar Bakkeby */
public class RP_ACK {

  private TPDU tpdu;
  private boolean isDeliver;

  /**
   * Constructor, takes an SMS-DELIVER-REPORT as argument.
   *
   * @param report The SMS-DELIVER-REPORT
   */
  public RP_ACK(final SMSDeliverReport report) {
    this.tpdu = report;
    isDeliver = true;
  }

  /**
   * Constructor, takes an SMS-SUBMIT-REPORT as argument.
   *
   * @param report The SMS-SUBMIT-REPORT
   */
  public RP_ACK(final SMSSubmitReport report) {
    this.tpdu = report;
    isDeliver = false;
  }

  /**
   * Constructor, creates the TPDU from a PDU string.
   *
   * @param pdu The PDU string
   * @throws IllegalArgumentException If argument is not a PDU
   */
  public RP_ACK(final String pdu) throws IllegalArgumentException {
    if (pdu.length() > 2) {
      int mtiValue = MTI.parse(Convert.hexToInt(pdu.substring(0, 2)), MTI.DIRECTION.MS_TO_SC).value;
      switch (mtiValue) {
      case 0:
        this.tpdu = new SMSDeliverReport(pdu, true);
        this.isDeliver = true;
        break;

      case 1:
        this.tpdu = new SMSSubmitReport(pdu, true);
        this.isDeliver = false;
        break;

      default:
        throw new IllegalArgumentException(pdu);
      }
    } else {
      throw new IllegalArgumentException(pdu);
    }
  }

  /**
   * This will return the TPDU.
   *
   * @return The TPDU
   */
  public TPDU getTPDU() {
    return tpdu;
  }

  /**
   * This will return SMS_DELIVER_REPORT or SMS_SUBMIT_REPORT.
   *
   * @return See MTI static field values
   */
  public MTI getMTI() {
    return (isDeliver ? MTI.SMS_DELIVER_REPORT : MTI.SMS_SUBMIT_REPORT);
  }
}